<template>
    <div>
        <common-header>
            <template v-slot:title>
                用户注册
            </template>
            <template v-slot:extra>
                <router-link to="/login">
                     <el-link>已有账号? 请登录</el-link>
                </router-link>  
            </template>
        </common-header>
        <el-divider></el-divider>
        <el-row style="margin-top:20px; min-height: 600px;">
            <el-col :span="8" :offset="8" class="register-panel">
                <el-steps :space="280" style="width: 100%;margin-bottom: 28px;" 
                    align-center :active="step" finish-status="success">
                    <!-- <el-step title="验证手机号"></el-step> -->
                    <el-step title="填写账号信息"></el-step>
                    <el-step title="注册完成"></el-step>
                </el-steps> 
                <!-- <el-card style="width: 100%;" v-show="step == -1">
                    <el-form :model="registerForm" ref="registerForm" class="demo-ruleForm">
                        <el-form-item>
                            <el-input placeholder="请输入手机号" v-model="registerForm.phone">
                                <template slot="prepend">
                                    <i class="fa fa-phone"></i>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-col :span="18">
                                <el-input placeholder="请输验证码" v-model="registerForm.validateCode">
                                    <template slot="prepend">
                                        <i class="fa fa-comment"></i>
                                    </template>
                                </el-input>
                            </el-col>
                            <el-col :span="6">
                                <el-button style="float: right; width: 92%;" type="success">获取验证码</el-button>
                            </el-col>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="danger" @click="step = 1" style="width: 100%;">下一步</el-button>
                        </el-form-item>
                    </el-form>
                </el-card> -->
                <el-card style="width: 100%;" v-show="step == -1">
                    <el-form :model="registerForm" ref="registerForm" class="demo-ruleForm" :rules="rules">
                        <el-form-item prop="username">
                            <el-input placeholder="请输入用户名"  v-model="registerForm.username">
                                <template slot="prepend">
                                    <i class="fa fa-user"></i>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input placeholder="请输入密码" ref="gainOne" type="password"  v-model="registerForm.password">
                                <template slot="prepend">
                                    <i class="fa fa-lock"></i>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="repassword">
                            <el-input placeholder="请输入重复密码" ref="gainTwo" type="password"  v-model="registerForm.repassword">
                                <template slot="prepend">
                                    <i class="fa fa-lock"></i>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="email">
                            <el-input placeholder="请输入邮箱" v-model="registerForm.email">
                                <template slot="prepend">
                                    <i class="fa fa-envelope"></i>
                                </template>
                            </el-input>
                        </el-form-item>
                        <!-- <el-form-item>
                            <el-input placeholder="请输入生日" v-model="registerForm.repassword">
                                <template slot="prepend">
                                    <i class="fa fa-birthday-cake"></i>
                                </template>
                            </el-input>
                        </el-form-item> -->
                        <el-form-item>
                            <el-button type="danger" :loading="loading"  @click.prevent="register" style="width: 100%;" >注册</el-button>
                            <!-- TODO:下一步后要跳转,并设置下一步按钮无法点击,注册要判断用户名是否存在 -->
                        </el-form-item>
                    </el-form>
                </el-card>
                <el-result v-show="step == 3" icon="success" title="注册成功">
                    <template v-slot:subTitle>
                        页面将在 <span style="color: #F56C6C;">{{timer}}s</span> 后跳转到首页
                    </template>
                </el-result>
            </el-col>
        </el-row>
        <el-divider></el-divider>
        <Footer></Footer>
    </div>
</template>

<script>
import http from '@/utils/request'
import CommonHeader from '@/components/CommonHeader'
import Footer from '@/components/Index/Footer'

export default {
    components: {
        CommonHeader,
        Footer
    },
    data() {
        // 重复密码验证
        const pwdAgainCheck = async(rule, value, callback) => {
            if (value.length < 1) {
                return callback(new Error('重复密码不能为空！'));
            } else if(this.registerForm.password !== this.registerForm.repassword){
                // var CheckStatus = 1
                // while(CheckStatus == 1){
                //     this.$refs.gainOne.focus();
                //     this.$refs.gainOne.blur();
                //     this.$refs.gainTwo.focus();
                //     this.$refs.gainTwo.blur();
                //     CheckStatus = 0
                // }
                // this.$nextTick( () =>{
                //     this.$refs.gainOne.focus()
                // }, 100)
                // this.$refs.gainTwo.focus();
                return callback(new Error('两次输入密码不一致！'));
            }else {
                callback()
            }
        }
        return {
            regx: /^\w[-\w]{2,}@[a-zA-Z0-9]{2,}\.[a-zA-Z]{2,}$/,
            step: -1,
            timer: 5,
            loading: false,
            registerForm: {
                // phone: '',
                // validateCode: '',
                username: '',
                password: '',
                repassword: '',
                email: '',
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'change' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    // { required: true, validator: pwdAgainCheck, trigger: 'blur'}
                ],
                repassword: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { required: true, validator: pwdAgainCheck, trigger: 'blur'}
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { pattern: /^\w[-\w]{2,}@[a-zA-Z0-9]{2,}\.[a-zA-Z]{2,}$/, message: '请输入正确的邮箱格式', trigger: 'change'}
                    // TODO: 邮箱验证
                ]
            }
            
        }
    },
    methods: {
        register() {
            if(this.registerForm.username == '' || this.registerForm.password == '' || this.registerForm.repassword == '' || this.registerForm.email == ''){
                this.loading = true
                this.$message.error({
                                message: '注册内容不能为空，请填写完整后注册',
                                duration: 5000,
                            });
                            setTimeout(() => {
                                this.loading = false
                            }, 3000);
            }
            else if(this.registerForm.password != this.registerForm.repassword) {
                this.loading = true
                this.$message.error({
                                message: '密码输入不一致，请重新输入',
                                duration: 5000,
                            });
                            setTimeout(() => {
                                this.loading = false
                            }, 3000);
            }else if(!this.registerForm.email.match(this.regx)) {
                this.loading = true
                this.$message.error({
                                message: '邮箱格式有误，请重新输入',
                                duration: 5000,
                            });
                            setTimeout(() => {
                                this.loading = false
                            }, 3000);
            }else{
                http.post('/register', this.registerForm)
                .then(res => {
                        this.loading = true
                        if(res.data.code > 0) {  // 登录成功
                            this.step = 3
                            window.setInterval(() => {
                            this.timer--
                            if(this.timer == 0){
                                this.$router.push('/login')  // 跳转到首页
                            }
                            },1000)

                        }else {  // 登录失败
                            this.$message.error({
                                message: '用户名已存在,请修改用户名',
                                duration: 5000,
                            });
                            setTimeout(() => {
                                this.loading = false
                            }, 3000);
                    }
                })
            } 
        }
    }
}
</script>

<style scoped>
    .register-panel {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .el-steps {
        justify-content: center
    }
</style>